﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLogicLayer;

namespace PresentationLayer
{
    public partial class frmTranslation : Form
    {
        #region Properties
        private frmMain MdiForm { get; set; }
        private Int32 IDTranslations { get; set; }
        private enumState State { get; set; }
        enum enumState
        {
            View,
            New,
            Edit
        }
        #endregion

        #region Constructors
        public frmTranslation(frmMain pMdiForm)
        {
            InitializeComponent();
            this.MdiForm = pMdiForm;
        }
        #endregion

        #region Events
        private void frmTranslation_Load(object sender, EventArgs e)
        {
            this.MdiForm.Event_OnTranslationChanged += new frmMain.OnTranslationChangedHandler(Translate);
            Translate();
            FillComboBoxLanguages();
            FillListViewForms();
            SetButtonsToState(enumState.View);
            SetTextBoxToState(enumState.View);
            CloseIfNoLanguagesAvailable();
        }

        private void lsvForms_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillListViewControls();
        }

        private void lsvControls_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillFieldsWithControlDetails();
        }

        private void cboLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillFieldsWithControlDetails();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            SetButtonsToState(enumState.View);
            SetTextBoxToState(enumState.View);
            FillFieldsWithControlDetails();
            lsvControls.Focus();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            SetButtonsToState(enumState.Edit);
            SetTextBoxToState(enumState.Edit);
            txtTranslation.Focus();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }
        #endregion

        #region Public Methods
        #endregion

        #region Private Methods
        /// <summary>
        /// Checks if there are languages available. If not, the form is closed.
        /// </summary>
        private void CloseIfNoLanguagesAvailable()
        {
            if (cboLanguage.SelectedItem == null)
            {
                MessageBox.Show(this.MdiForm.DataAccess.GetTranslation(this.Name, "CloseIfNoLanguagesAvailable()", "No languages available.", 0));
            }
        }

        /// <summary>
        /// Initializes the forms ListView.
        /// </summary>
        private void InitializeListViewForms()
        {
            if (lsvForms.Columns.Count > 0)
            {
                lsvForms.Columns.Clear();
            }
            lsvForms.Columns.Add(this.MdiForm.DataAccess.GetTranslation(this.Name, lsvForms.Name + "_Column1_Form", "Form", 0));
            lsvForms.View = View.Details;
            lsvForms.FullRowSelect = true;
            lsvForms.HideSelection = false;
            lsvForms.MultiSelect = false;
        }

        /// <summary>
        /// Initializes the controls ListView.
        /// </summary>
        private void InitializeListViewControls()
        {
            if (lsvControls.Columns.Count > 0)
            {
                lsvControls.Columns.Clear();
            }
            lsvControls.Columns.Add(this.MdiForm.DataAccess.GetTranslation(this.Name, lsvControls.Name + "_Column1_Control", "Control", 0));
            lsvControls.View = View.Details;
            lsvControls.FullRowSelect = true;
            lsvControls.HideSelection = false;
            lsvControls.MultiSelect = false;
        }

        /// <summary>
        /// Translates every visible element.
        /// </summary>
        private void Translate()
        {
            this.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, "Text", this.Text, 0);
            grpLanguage.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, grpLanguage.Name, grpLanguage.Text, 0);
            grpSearch.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, grpSearch.Name, grpSearch.Text, 0);
            grpDetails.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, grpDetails.Name, grpDetails.Text, 0);
            lblForm.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblForm.Name, lblForm.Text, 0);
            lblControl.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblControl.Name, lblControl.Text, 0);
            lblOriginal.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblOriginal.Name, lblOriginal.Text, 0);
            lblTranslation.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblTranslation.Name, lblTranslation.Text, 0);
            lblDateCreated.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblDateCreated.Name, lblDateCreated.Text, 0);
            lblDateChanged.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblDateChanged.Name, lblDateChanged.Text, 0);
            grpOptions.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, grpOptions.Name, grpOptions.Text, 0);
            btnCancel.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, btnCancel.Name, btnCancel.Text, 0);
            btnEdit.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, btnEdit.Name, btnEdit.Text, 0);
            btnSave.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, btnSave.Name, btnSave.Text, 0);
            InitializeListViewForms();
            InitializeListViewControls();
        }

        /// <summary>
        /// Fills the languages ComboBox with all languages except the system language English.
        /// </summary>
        private void FillComboBoxLanguages()
        {
            if(cboLanguage.Items.Count > 0)
            {
                cboLanguage.Items.Clear();
            }

            ArrayList Languages = this.MdiForm.DataAccess.GetAllLanguages();
            foreach (clsLanguage Language in Languages)
            {
                if (Language.ID != 1)
                {
                    cboLanguage.Items.Add(Language);
                }
            }

            if (cboLanguage.Items.Count > 0)
            {
                cboLanguage.SelectedIndex = 0;
            }
        }

        /// <summary>
        /// Fills the ListView with forms.
        /// </summary>
        /// <param name="pSearchArgument"></param>
        private void FillListViewForms()
        {
            //Delete all ListView items if available.
            if (lsvForms.Items.Count > 0)
            {
                lsvForms.Items.Clear();
            }
            //Get all forms.
            ArrayList Forms = this.MdiForm.DataAccess.GetAllForms();
            for (Int32 i = 0; i < Forms.Count; i++)
            {
                //Get form.
                clsForm FormRow = (clsForm)Forms[i];
                //Create item for ListView.
                ListViewItem Item = new ListViewItem(FormRow.FormName);
                Item.Tag = FormRow;
                //Add item to ListView.
                lsvForms.Items.Add(Item);
            }
            //Resize columns adapted to contents.
            lsvForms.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
            //Select the first item if possible.
            if (lsvForms.Items.Count > 0)
            {
                lsvForms.Items[0].Selected = true;
            }
            else
            {
                FillListViewControls();
            }
        }

        /// <summary>
        /// Get the form information from the ListView and fill the details into the controls ListView.
        /// </summary>
        private void FillListViewControls()
        {
            if (lsvForms.SelectedItems.Count > 0)
            {
                clsForm FormRow = (clsForm)lsvForms.SelectedItems[0].Tag;

                //Delete all ListView items if available.
                if (lsvControls.Items.Count > 0)
                {
                    lsvControls.Items.Clear();
                }
                //Get all controls in the specific form.
                ArrayList Controls = this.MdiForm.DataAccess.GetControls(FormRow.FormName);
                for (Int32 i = 0; i < Controls.Count; i++)
                {
                    //Get control.
                    clsControl ControlRow = (clsControl)Controls[i];
                    //Create item for ListView.
                    ListViewItem Item = new ListViewItem(ControlRow.ControlName);
                    Item.Tag = ControlRow;
                    //Add item to ListView.
                    lsvControls.Items.Add(Item);
                }
                //Resize columns adapted to contents.
                lsvControls.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
                //Select the first item if possible.
                if (lsvControls.Items.Count > 0)
                {
                    lsvControls.Items[0].Selected = true;
                }
                else
                {
                    FillFieldsWithControlDetails();
                }
            }
            else
            {
                //Delete all ListView items if available.
                if (lsvControls.Items.Count > 0)
                {
                    lsvControls.Items.Clear();
                }

                SetButtonsToState(enumState.View);
                SetTextBoxToState(enumState.View);
            }
        }

        /// <summary>
        /// Fill the fields with the selection details.
        /// </summary>
        private void FillFieldsWithControlDetails()
        {
            Boolean EmptyFields = true;

            if (lsvForms.SelectedItems.Count > 0 && lsvControls.SelectedItems.Count > 0 && cboLanguage.SelectedItem != null)
            {
                clsForm FormRow = (clsForm)lsvForms.SelectedItems[0].Tag;
                clsControl ControlRow = (clsControl)lsvControls.SelectedItems[0].Tag;
                clsLanguage Language = (clsLanguage)cboLanguage.SelectedItem;

                clsControl ControlDetailsOriginal = this.MdiForm.DataAccess.GetControl(FormRow.FormName, ControlRow.ControlName, 1);
                lblOriginalVal.Text = ControlDetailsOriginal.Translation;

                clsControl ControlDetails = this.MdiForm.DataAccess.GetControl(FormRow.FormName, ControlRow.ControlName, Language.ID);

                if (ControlDetails.ControlName != null)
                {
                    this.IDTranslations = ControlDetails.ID;
                    lblFormVal.Text = ControlDetails.FormName;
                    lblControlVal.Text = ControlDetails.ControlName;
                    txtTranslation.Text = ControlDetails.Translation;
                    lblDateCreatedVal.Text = ControlDetails.DateCreated.ToString();
                    lblDateChangedVal.Text = ControlDetails.DateChanged.ToString();
                    lblCreatedBy.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblCreatedBy.Name, "by", 0);
                    lblChangedBy.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblChangedBy.Name, "by", 0);
                    lblUserCreatedVal.Text = ControlDetails.UserCreated;
                    lblUserChangedVal.Text = ControlDetails.UserChanged;
                    EmptyFields = false;
                }
            }
            else
            {
                lblOriginalVal.Text = "";
            }

            if(EmptyFields)
            {
                lblFormVal.Text = "";
                lblControlVal.Text = "";
                txtTranslation.Text = "";
                lblDateCreatedVal.Text = "";
                lblDateChangedVal.Text = "";
                lblCreatedBy.Text = "";
                lblChangedBy.Text = "";
                lblUserCreatedVal.Text = "";
                lblUserChangedVal.Text = "";
            }

            SetButtonsToState(enumState.View);
            SetTextBoxToState(enumState.View);
        }

        /// <summary>
        /// Applies the state to the buttons.
        /// </summary>
        /// <param name="pState">The state.</param>
        private void SetButtonsToState(enumState pState)
        {
            this.State = pState;
            switch (this.State)
            {
                case enumState.View:
                    btnCancel.Enabled = false;
                    btnSave.Enabled = false;
                    if (lsvControls.SelectedItems.Count > 0)
                    {
                        if (lsvControls.SelectedItems[0].Index > -1)
                        {
                            if (cboLanguage.SelectedItem != null)
                            {
                                btnEdit.Enabled = true;
                            }
                            else
                            {
                                btnEdit.Enabled = false;
                            }
                        }
                        else
                        {
                            btnEdit.Enabled = false;
                        }
                    }
                    else
                    {
                        btnEdit.Enabled = false;
                    }
                    break;
                case enumState.New:
                    btnCancel.Enabled = true;
                    btnEdit.Enabled = false;
                    btnSave.Enabled = true;
                    break;
                case enumState.Edit:
                    btnCancel.Enabled = true;
                    btnEdit.Enabled = false;
                    btnSave.Enabled = true;
                    break;
            }
        }

        /// <summary>
        /// Applies the state to the TextBoxes.
        /// </summary>
        /// <param name="pState">The state.</param>
        private void SetTextBoxToState(enumState pState)
        {
            this.State = pState;
            switch (this.State)
            {
                case enumState.View:
                    if (cboLanguage.SelectedItem != null)
                    {
                        cboLanguage.Enabled = true;
                        lsvForms.Enabled = true;
                        lsvControls.Enabled = true;
                    }
                    else
                    {
                        cboLanguage.Enabled = false;
                        lsvForms.Enabled = false;
                        lsvControls.Enabled = false;
                    }
                    txtTranslation.Enabled = false;
                    break;
                case enumState.New:
                    cboLanguage.Enabled = false;
                    lsvForms.Enabled = false;
                    lsvControls.Enabled = false;
                    txtTranslation.Enabled = true;
                    txtTranslation.Text = "";
                    lblDateCreatedVal.Text = "";
                    lblDateChangedVal.Text = "";
                    lblUserCreatedVal.Text = "";
                    lblUserChangedVal.Text = "";
                    lblCreatedBy.Text = "";
                    lblChangedBy.Text = "";
                    break;
                case enumState.Edit:
                    cboLanguage.Enabled = false;
                    lsvForms.Enabled = false;
                    lsvControls.Enabled = false;
                    txtTranslation.Enabled = true;
                    break;
            }
        }

        /// <summary>
        /// Saves the translation.
        /// </summary>
        private void Save()
        {
            if (SaveTranslation())
            {
                FillFieldsWithControlDetails();
                SetButtonsToState(enumState.View);
                SetTextBoxToState(enumState.View);
                this.MdiForm.Translate();
                lsvControls.Focus();
            }
        }

        /// <summary>
        /// Saves the translation depending on the actual state.
        /// </summary>
        /// <returns>State of success.</returns>
        private Boolean SaveTranslation()
        {
            Boolean Success = false;
            if (CheckIfTextBoxesEmpty() == false)
            {
                clsForm FormRow = (clsForm)lsvForms.SelectedItems[0].Tag;
                clsControl ControlRow = (clsControl)lsvControls.SelectedItems[0].Tag;
                clsLanguage Language = (clsLanguage)cboLanguage.SelectedItem;

                if (this.MdiForm.DataAccess.TranslationExists(FormRow.FormName, ControlRow.ControlName, Language.ID) == false)
                {
                    Success = this.MdiForm.DataAccess.InsertTranslation(FormRow.FormName, ControlRow.ControlName, Language.ID, txtTranslation.Text, this.MdiForm.DataAccess.Settings.User_Login.ID);
                }
                else
                {
                    Success = this.MdiForm.DataAccess.ChangeTranslation(this.IDTranslations, FormRow.FormName, ControlRow.ControlName, txtTranslation.Text);
                }
            }
            if (Success == false)
            {
                MessageBox.Show("Error saving translation.");
            }
            return Success;
        }

        /// <summary>
        /// Checks the emptyness of the TextBoxes.
        /// </summary>
        /// <returns>True if TextBoxes are empty.</returns>
        private Boolean CheckIfTextBoxesEmpty()
        {
            if (txtTranslation.Text == "")
            {
                return true;
            }

            return false;
        }
        #endregion
    }
}
